<?php

!defined('IN_NOVA') && exit('Access Denied!');

class Theme {

	public $theme;

	function __construct() {
		$this->theme['js_head'] = array();
		$this->theme['js_foot'] = array();
		$this->theme['css'] = array();
	}

    function __set( $name, $value ) {
        $this->theme[$name] = $value;
    }

    function __get( $name ) {
    	//[DEBUG]
		global $log;
		//[/DEBUG]
		if( !isset( $this->theme[$name] ) ) {
			//[DEBUG]
			if( defined( 'APP_DEBUG' ) && APP_DEBUG === TRUE ) {
				$log->add( '[' . __FILE__ . '] [' . __LINE__ . '] ' . L( '_USE_ILLEGAL_INDEX_' ) . ' ' . $name, E_USER_NOTICE );
			}
			//[/DEBUG]
			return NULL;
		}
        return $this->theme[$name];
    }

	// 载入模板
	public function load( $tpl, $ext = 'php' ) {
		global $db,$request,$cache;
		$style = $this->style;
		if( $style == NULL ) {
			$style = $cache->config['style'];
		}
		$tpl_file = APP_THEME . $style . '/' . $tpl . '.' . $ext;
		if ( file_exists( $tpl_file ) ) {
			include( $tpl_file );
		} else {
			$tpl_file =  APP_THEME . 'template/' . $tpl . '.' . $ext;
			if( file_exists( $tpl_file ) ) {
				include( $tpl_file );
			}else{
				throw new NovaException( '模板文件 ' . $tpl . '.' . $ext . ' 不存在！', E_ERROR );
			}
		}
	}

	// 检查风格包是否合法
	static public function is_legal( $each_dir ) {
		if( !file_exists( APP_THEME . $each_dir . '/info.xml' ) ) return false;
		if( !file_exists( APP_THEME . $each_dir . '/index.php' ) ) return false;
		if( !file_exists( APP_THEME . $each_dir . '/page.php' ) ) return false;
		return true;
	}

	// 读取 xml 配置文件
	static public function parse_xml_info( $xml_file,$type ) {
		$fp = fopen($xml_file, 'rb');
		$content = fread($fp, filesize($xml_file));
		preg_match_all('/<name>(.+?)<\/name>/is',$content, $name);
		preg_match_all('/<author>(.*?)<\/author>/is',$content, $author);
		preg_match_all('/<version>(.*?)<\/version>/is',$content, $version);
		preg_match_all('/<pubdate>(.*?)<\/pubdate>/is',$content, $pubdate);
		preg_match_all('/<homepage>(.*?)<\/homepage>/is',$content, $homepage);
		preg_match_all('/<thumb>(.*?)<\/thumb>/is',$content, $thumb);
		preg_match_all('/<intro>(.*?)<\/intro>/is',$content, $intro);
		$info = array(
			'name' => $name[1][0],
			'author' => $author[1][0],
			'version' => $version[1][0],
			'pubdate' => $pubdate[1][0],
			'homepage' => $homepage[1][0],
			'thumb' => $thumb[1][0],
			'intro' => $intro[1][0],
		);
		return $info;
	}

	// 404
	public function err_404() {
		@header( 'HTTP/1.1 404 Not Found' );
		@header( 'status: 404 Not Found' );
		$this->load('404');
	}

	// keyword 标签
	public function get_keywords() {
		global $cache;
		if( isset( $this->theme['keywords'] ) && $this->theme['keywords'] != '' ) {
			return $this->theme['keywords'];
		}else{
			return $cache->config['keywords'];
		}
	}

	// description 标签
	public function get_description() {
		global $cache;
		if( isset( $this->theme['description'] ) && $this->theme['description'] != '' ) {
			return $this->theme['description'];
		}else{
			return $cache->config['description'];
		}
	}

	// 页面标题
	public function get_title() {
		global $cache;
		if ( isset( $this->theme['title'] ) ) {
			return $this->theme['title'] . ' « ' . $cache->config['blogname'];
		} else {
			return $cache->config['blogname'] . ' &raquo; ' . $cache->config['description'];
		}
	}

	// 博客信息
	public function get_info( $index ) {
		global $cache;
		//[DEBUG]
		global $log;
		//[/DEBUG]
		switch( $index ) {
		case 'path':
			return APP_PATH;
		case 'theme_path':
			return THEME_PATH . $this->get_info( 'style' ) . '/';
		case 'style':
			if( $this->style == NULL ) {
				$this->style = $cache->config['style'];
			}
			return $this->style;
		case 'version':
			return XKLOG_VERSION;
		default:
			if ( array_key_exists( $index, $cache->config ) ) {
				return $cache->config[$index];
			} else {
				//[DEBUG]
				if( defined( 'APP_DEBUG' ) && APP_DEBUG === TRUE ) {
					$log->add( '[' . __FILE__ . '] [' . __LINE__ . '] ' . L( '_USE_ILLEGAL_INDEX_' ) . ' ' . $index, E_USER_NOTICE );
				}
				//[/DEBUG]
				return NULL;
			}
		}
	}

	//js
	public function get_js( $index ) {
		global $cache;
		$output = '';
		if( $index == 'head' ) {
			$output .= "<script language=\"javascript\" type=\"text/javascript\" src=\"" . THEME_PATH . "script/jquery.js\"></script>";
			$output .= "<script language=\"javascript\" type=\"text/javascript\" src=\"" . THEME_PATH . "script/common.js\"></script>";
			foreach( $this->theme['js_head'] as $js ) {
				$output .= "<script language=\"javascript\" type=\"text/javascript\" src=\"$js\"></script>";
			}
			$output .= "<script language=\"javascript\" type=\"text/javascript\">var BLOG_URL = '" . $cache->config['domain'] . "';var BLOG_PATH= '" . APP_PATH . "' ;</script>";
		} else {
			foreach( $this->theme['js_foot'] as $js ) {
				$output .= "<script language=\"javascript\" type=\"text/javascript\" src=\"$js\"></script>";
			}
		}
		return $output;
	}

	public function get_css() {
		$output = '';
		foreach( $this->theme['css'] as $css ) {
			$output .= "<link rel=\"stylesheet\" href=\"$css\" type=\"text/css\" media=\"screen\" />";
		}
		return $output;
	}

	//运行信息
	public function get_run_info() {
		global $db,$_NOVA;
		$run_time = number_format( microtime( TRUE ) - $_NOVA['RUN_TIME']['NOVA_START'], 4 );
		if ( function_exists('ob_gzhandler') ) {
			$temp = 'Gzip enabled';
		} else {
			$temp = 'Gzip disabled';
		}
		return 'Processed in ' . $run_time * 1000 . ' ms, ' . $db->querynum . ' queries, ' . $temp;
	}

	// 菜单项目
	public function get_menu() {
		global $cache;
		$value = '';
		if( !isset( $this->theme['name'] ) ) $this->theme['name'] = '';
		foreach( $cache->menu as $ikey => $ivalue ) {
			if( $ivalue['type'] == 0 ){
				if( $this->theme['name'] == $ikey || ( $this->theme['name'] == '' && $ikey == 'index' ) ){
					$value .= '<li class="current_page"><a href="' . U( APP_PATH, $ivalue['address'], '' ) . '">' . $ivalue['title'] . '</a></li>';
				}else{
					$value .= '<li><a href="' . U( APP_PATH, $ivalue['address'], '' ) . '">' . $ivalue['title'] . '</a></li>';
				}
			}else{
				if( $this->theme['name'] == $ikey || ( $this->theme['name'] == '' && $ikey == 'index' ) ){
					$value .= '<li class="current_page"><a href="' . $ivalue['address'] . '" target="_blank">' . $ivalue['title'] . '</a></li>';
				}else{
					$value .= '<li><a href="' . $ivalue['address'] . '" target="_blank">' . $ivalue['title'] . '</a></li>';
				}
			}
		}
		return $value;
	}

	// 首页日志列表
	public function show_article_list( $id = 0, $type = 'index' ) {
		global $cache,$db;
		$offset = ( $this->current_page - 1 ) * $this->max_per_page;
		$temp = array();
		if ( $type == 'category' && array_key_exists( $id , $cache->category ) ) {
			$sql = "SELECT * FROM `" . DB_PREFIX . "article` WHERE isdel=0 AND category=$id ORDER BY istop DESC,id DESC LIMIT $offset, $this->max_per_page";
			$temp = $db->fetch_all( $sql );
			$article_num = $cache->category[$id]['articlenum'];
		} elseif( $type == 'tags' && array_key_exists( $id , $cache->tag ) ) {
			$sql = "SELECT * FROM `" . DB_PREFIX . "article` WHERE isdel=0 AND tag LIKE '%{*$id*}%' ORDER BY istop DESC,id DESC LIMIT $offset, $this->max_per_page";
			$temp = $db->fetch_all( $sql );
			$article_num = $cache->tag[$id]['t_num'];
		} else {
			if( $this->current_page == 1 ) {
				if( $cache->article_list == NULL ) {
					$temp = $db->fetch_all( "SELECT * FROM `" . DB_PREFIX . "article` WHERE isdel=0 ORDER BY istop DESC,id DESC LIMIT 0," . $this->max_per_page );
					foreach( $temp as $key => $value ) {
						if( $value['summary'] != '' ) {
							$value['content'] = $value['summary'];
						}
						$value['summary'] = '';
						$temp[$key] = $value;
					}
					$cache->set( 'article_list', $temp, 0 );
				} else {
					$temp = $cache->article_list;
				}
			} else {
				$sql = "SELECT * FROM `" . DB_PREFIX . "article` WHERE isdel=0 ORDER BY istop DESC,id DESC LIMIT $offset, $this->max_per_page";
				$temp = $db->fetch_all( $sql );
			}
			$article_num = $cache->count['article_num'];
		}
		foreach( $temp as $row => $value ){
			$this->theme['content'] = &$value;
			$this->load( 'article_index' );
		}
	}

	// 日志详细
	public function show_article_detail( $id ){
		global $db;
		$sql = "SELECT * FROM `" . DB_PREFIX . "article` WHERE isdel = 0 AND id=$id";
		$article = $db->fetch_one_array( $sql );
		if( $article == '' ) {
			throw new NovaException( '日志 id 非法！', E_ERROR );
		}
		$this->theme['content'] = &$article;
		$this->load('article_read');
	}

	// 日志详细
	public function get_article_content( $key ) {
		global $cache;
		switch( $key ) {
		case 'summary':
			if( $this->theme['content']['summary'] != '' ) {
		    	return $this->theme['content']['summary'];
			} else {
				return $this->theme['content']['content'];
			}
			break;
		case 'title':
			if( $this->theme['content']['istop'] ) {
				$this->theme['content']['title'] = '[置顶] ' . $this->theme['content']['title'];
			}
			return $this->theme['content']['title'];
			break;
		case 'url':
			if( $this->theme['content']['address'] == '' ) $this->theme['content']['address'] = $this->theme['content']['id'];
			if( $cache->config['arttype'] == '动态' ){
				$this->theme['content']['url'] = APP_PATH . 'index.php?m=article&p=' . $this->theme['content']['id'];
			}else{
				$this->theme['content']['url'] = APP_PATH . 'article/' . $cache->category[$this->theme['content']['category']]['alias'] . '/' . $this->theme['content']['address'] . '.html';
			}
			return $this->theme['content']['url'];
		case 'tag':
			if( $this->theme['content']['tag'] != '' ) {
				$tags = str_replace( '*}{*', ',', $this->theme['content']['tag'] );
				$tags = str_replace( '{*', '', $tags );
				$tags = str_replace( '*}', '', $tags );
				$temp = explode( ',' , $tags);
				$tags = '';
				foreach($temp as $row){
					$tags .= '<a href="' . U( APP_PATH, 'tags', urlencode( $cache->tag[$row]['t_name'] ) ) . '">' . $cache->tag[$row]['t_name'] . '</a>';
				}
				$tags = str_replace( '</a><a href=','</a> , <a href=', $tags );
				return $tags;
			} else {
				return '';
			}
		case 'category':
			return '<a href="' . U( APP_PATH, 'category', $cache->category[$this->theme['content']['category']]['alias'] ) . '" title="' . $cache->category[$this->theme['content']['category']]['description'] . '">' . $cache->category[$this->theme['content']['category']]['name'] . '</a>';
		default:
			if( isset( $this->theme['content'][$key] ) ) {
				return $this->theme['content'][$key];
			} else {
				return '';
			}
		}
	}

	public function get_article_path( $id, $type ) {
		global $cache;
		$output = '';
		switch( $type ) {
		case 'tags':
			// 首页 -> 标签云 -> XXXX
			$output = '<div id="postpath"><a href="' . APP_PATH . '" title="' . $cache->config['blogname'] . '">' . L( '_HOME_PAGE_' ) . '</a> &gt; ';
			$output .= '<a href="' . U( APP_PATH, 'tags' ) . '">' . L( '_TAGS_CLOUD_' ) . '</a> &gt; ';
			$output .= $cache->tag[$id]['t_name'] . '</div>';
			break;
		case 'category':
		case 'article':
			// 首页 -> 一级分类 -> 二级分类 -> ……
			$output = '<div id="postpath"><a href="' . APP_PATH . '" title="' . $cache->config['blogname'] . '">' . L( '_HOME_PAGE_' ) . '</a> &gt; ';
			$output .= $this->get_article_path_c( $cache->category[$id]['pid'] );
			break;
		}
		if( $type == 'article' ) {
			$output .= '<a href="' . U( APP_PATH, 'category', $cache->category[$id]['alias'] ) . '" title="' . $cache->category[$id]['description'] . '">' . $cache->category[$id]['name'] . '</a> &gt; ';
			$output .= $this->title . '</div>';
		} elseif( $type == 'category' ) {
			$output .= $cache->category[$id]['name'] . '</div>';
		}
		return $output;
	}

	private function get_article_path_c( $id ) {
		global $cache;
		if( $id == 0 ) {
			return '';
		}
		$output = '<a href="' . U( APP_PATH, 'category', $cache->category[$id]['alias'] ) . '" title="' . $cache->category[$id]['description'] . '">' . $cache->category[$id]['name'] . '</a> &gt; ';
		$output = $this->get_article_path_c( $cache->category[$id]['pid'] ) . $output;
		return $output;
	}

	public function get_announcement( $module ) {
		$output = '';
		if( $module == 'index' ) {
			global $cache;
			if( $cache->laodao === NULL ) {
				global $db;
				$cache->set( 'laodao', $db->result("SELECT ld_laodao FROM `" . DB_PREFIX . "laodao` ORDER BY ld_id DESC LIMIT 0,1"), 0 );
			}
			if( is_string( $cache->laodao ) && $cache->laodao != '' ) {
				$output = '<div id="announcement">' . str_replace( array("\r\n", "\n", "\r"),'<br />',htmlspecialchars( $cache->laodao ) ) . '</div>';
			}
		}
		return $output;
	}

	// 风格选择
	public function get_style( $a_tag = '<li>', $b_tag = '</li>' ) {
		global $cache;
		$output = '';
		$cstyle = $this->style;
		if( $cstyle == NULL ) {
			$cstyle = $cache->config['style'];
		}
		foreach( $cache->style as $style ) {
			if( $style['directory'] == $cstyle ) {
				$output .= $a_tag . $style['name'] . $b_tag;
			}else{
				$output .= $a_tag . '<a href="' . U( APP_PATH, 'index', $style['directory'] ) . '">' . $style['name'] . '</a>' . $b_tag;
			}
		}
		return $output;
	}

	// 日志分类
	public function get_category( $pid = 0, $showrss = TRUE, $str_len = 30 ) {
		global $cache;
		$output = '<ul>';
		foreach ( $cache->category as $row ) {
			if( $row['pid'] != $pid ) continue;
			if( $showrss == TRUE ) {
				$rssicon = ' [<a href="' . U( APP_PATH, 'rss', $row['cid'] ) . '">RSS</a>]';
			} else {
				$rssicon = '';
			}
			$output .= '<li><a href="' . U( APP_PATH ,'category', $row['alias'] ) . '" title="' . $row['description'] . '">' . cut_str( $row['name'],$str_len ) . '(' . $row['articlenum'] . ')</a>' . $rssicon . $this->get_category( $row['cid'] , $showrss, $str_len - 2 ) . '</li>';
		}
		if( $output == '<ul>' ) {
			$output = '';
		} else {
			$output .= '</ul>';
		}
		return $output;
	}

	// 最新评论
	public function get_comment_new( $str_len = 30 ) {
		global $cache;
		$output = '';
		if( $cache->comment_new == NULL ) {
			global $db;
			$cache->set( 'comment_new', $db->fetch_all( 'SELECT C.cid,C.comment, C.articleid, A.type, A.address, A.category, A.title, C.author FROM `' . DB_PREFIX . 'comment` as C, `' . DB_PREFIX . 'article` as A WHERE C.articleid=A.id and C.isdel=0 and C.isshow=1 and A.isdel=0 ORDER BY C.cid DESC limit 0,12' ), 0 );
		}
		foreach ( $cache->comment_new as $row ) {
			if( $row['address'] == '' ) $row['address'] = $row['articleid'];
			if( $cache->config['arttype'] == '动态' ){
				$output .= '<li><a href="' . APP_PATH . 'index.php?m=article&p=' . $row['articleid'] . '#comment-' . $row['cid'] . '" title="[' . $row['author'] . '] 发表在 [' . strip_tags( $row['title'] ) . ']">' . cut_str( str_replace( '<br />', ' ', $row['comment'] ), $str_len ) . '</a></li>';
			} else {
				$output .= '<li><a href="' . APP_PATH . 'article/' . $cache->category[$row['category']]['alias'] . '/' . $row['address'] . '.html#comment-' . $row['cid'] . '" title="[' . $row['author'] . '] 发表在 [' . strip_tags( $row['title'] ) . ']">' . cut_str( str_replace( '<br />', ' ', $row['comment'] ), $str_len ) . '</a></li>';
			}
		}
		echo $output;
	}

	// 提示信息
	public function show_message( $msg, $url = '', $litime = 3000 ) {
		global $_ENV;
		if( is_array($msg) ){
			$temp = '<ol>';
			foreach($msg as $row => $value){
				$temp .= '<li>' . $value . '</li>';
			}
			$temp .= '</ol>';
			$msg = $temp;
			unset($temp);
		}
		if( $url == '' ){
			$_ENV['message'] = array(
				'content' => $msg,
				'goto'     => '',
			);
		}else{
			$_ENV['message'] = array(
				'content' => $msg . "<script>setTimeout(\"window.location.href='" . $url . "';\",{$litime});</script>",
				'goto'     => '<a href="' . $url . '">如果您的浏览器没自动跳转，请点击这里</a>',
			);
		}
		$this->load( 'message' );
	}

	// 最新、随机、热门文章
	public function get_article_li( $type = 'new', $str_len = 30 ) {
		global $cache,$db;
		$output = '';
		if( $type == 'new' ){
			$temp = $cache->article_new;
			if( $temp == NULL ) {
				$content = $db->fetch_all( 'SELECT id,title,type,address,category FROM `' . DB_PREFIX . 'article` WHERE isdel=0 AND `type`<3 ORDER BY id DESC limit 0,6' );
				$cache->set( 'article_new', $content, 0 );
				$temp = $content;
				unset( $content );
			}
		}elseif( $type == 'hot' ){
			$temp = $cache->article_hot;
			if( $temp == NULL ) {
				$content = $db->fetch_all( 'SELECT id,title,type,address,category FROM `' . DB_PREFIX . 'article` WHERE isdel=0 AND `type`<3 ORDER BY comment DESC,`read` DESC limit 0,6' );
				$cache->set( 'article_hot', $content, 0 );
				$temp = $content;
				unset( $content );
			}
		}else{
			$temp = $cache->article_rnd;
			if( $temp == NULL ) {
				$content = $db->fetch_all( 'SELECT id,title,type,address,category FROM `' . DB_PREFIX . 'article` WHERE isdel=0 AND `type`<3 ORDER BY rand() limit 0,6' );
				// 随机日志缓存30秒
				$cache->set( 'article_rnd', $content, 30 );
				$temp = $content;
				unset( $content );
			}
		}
		if( isset( $temp ) ) {
			foreach ( $temp as $row) {
				if( $row['address'] == '' ) $row['address'] = $row['id'];
				if( $cache->config['arttype'] == '动态' ){
					$output .= '<li><a href="' . APP_PATH . 'index.php?m=article&p=' . $row['id'] . '" title="' . strip_tags( $row['title'] ) . '">'.cut_str( $row['title'],$str_len ).'</a></li>';
				} else {
					$output .= '<li><a href="' . APP_PATH . 'article/' . $cache->category[$row['category']]['alias'] . '/' . $row['address'] . '.html" title="' . strip_tags( $row['title'] ) . '">'.cut_str( $row['title'],$str_len ).'</a></li>';
				}
			}
			return $output;
		}
	}

	// 友情链接
	public function get_link( $str_len = 30, $a_tag = '<li>', $b_tag = '</li>' ) {
		global $cache;
		$output = '';
		if( $cache->link == NULL ) {
			global $db;
			$cache->set( 'link', $db->fetch_all( 'SELECT title,description,address FROM `' . DB_PREFIX . 'link` WHERE isdel=0 ORDER BY lid Asc' ), 0 );
		}
		foreach ( $cache->link as $row) {
			$output .= $a_tag . '<a href="' . $row['address'] . '" title="' . $row['description'] . '" target="_blank">' . cut_str( $row['title'],$str_len ) . '</a>' . $b_tag;
		}
		return $output;
	}

	// 统计信息
	public function get_count( $a_tag = '<li>', $b_tag = '</li>' ) {
		global $cache;
		$output = $a_tag . '建站时间: ' . date( 'Y年m月d日', $cache->config['builddate'] ) . $b_tag;
		$output .= $a_tag . '日志: ' . $cache->count['article_num'] . ' 篇' . $b_tag;
		$output .= $a_tag . $cache->menu['laodao']['title'] . ': ' . $cache->count['laodao_num'] . ' 条' . $b_tag;
		$output .= $a_tag . '评论: ' . $cache->count['comment_num'] . ' 篇' . $b_tag;
		$output .= $a_tag . '留言: ' . $cache->count['guestbook_num'] . ' 篇' . $b_tag;
		$output .= $a_tag . '附件: ' . $cache->count['file_num'] . ' 个' . $b_tag;
		return $output;
	}

	// 分页
	public function get_navigator( $article_num, $module, $params = '', $extra = '' ){
		if( $article_num < $this->max_per_page + 1 ) return;

		if( ( $article_num % $this->max_per_page ) == 0 ){
			$page_num = floor( $article_num / $this->max_per_page );
		}else{
			$page_num = floor( $article_num / $this->max_per_page ) + 1;
		}
		if( $this->current_page > $page_num ) return;

		if( $module == '' ) $module = 'index';

		$leader = '<div class = "navigator"><span class = "navigator_tip">分页：' . $this->current_page . '/' . $page_num . '</span>';

		$i = 1;
		$leader .= '<a href = "' . $this->get_navigator_url( $i, $module, $params, $extra ) . '">«</a>';
		if( $this->current_page != 1 ){
			$i = $this->current_page - 1;
		}
		$leader .= '<a href = "' . $this->get_navigator_url( $i, $module, $params, $extra ) . '">‹</a>';

		if( $this->current_page > 5 ){
			$leader .= ' …… ';
			for( $i = $this->current_page - 4 ; $i < $this->current_page ; $i ++ ){
				$leader .= '<a href="' . $this->get_navigator_url( $i, $module, $params, $extra ) . '">' . $i . '</a>';
			}
		}else{
			for($i = 1 ; $i < $this->current_page ; $i ++ ){
				$leader .= '<a href="' . $this->get_navigator_url( $i, $module, $params, $extra ) . '">' . $i . '</a>';
			}
		}

		$leader .= '<span class="navigator_current">' . $this->current_page . '</span>';

		if( $page_num - $this->current_page > 4 ){
			for($i = $this->current_page + 1 ; $i < $this->current_page + 5 ; $i ++ ){
				$leader .= '<a href="' . $this->get_navigator_url( $i, $module, $params, $extra ) . '">' . $i . '</a>';
			}
			$leader .= ' …… ';
		}else{
			for( $i = $this->current_page + 1 ; $i < $page_num + 1 ; $i ++ ){
				$leader .= '<a href="' . $this->get_navigator_url( $i, $module, $params, $extra ) . '">' . $i . '</a>';
			}
		}

		if( $this->current_page > $page_num - 1 ){
			$i = $page_num;
		}else{
			$i = $this->current_page + 1;
		}
		$leader .= '<a href = "' . $this->get_navigator_url( $i, $module, $params, $extra ) . '">›</a>';
		$leader .= '<a href = "' . $this->get_navigator_url( $page_num, $module, $params, $extra ) . '">»</a></div>';

		return $leader;
	}

	private function get_navigator_url( $i, $module = 'index', $params = '', $extra = '' ) {
		if( $params == '' ) {
			$params = $i;
		} else {
			if( $extra == '' ) {
				$extra = array( 'page' => $i );
			} else {
				if( is_array( $extra ) ) {
					$extra = array_merge( $extra, array( 'page' => $i ) );
				}
			}
		}
		$url = U( APP_PATH, $module, $params, $extra );
		return $url;
	}

	public function get_relate_article( $aid ) {
		global $db,$cache;
		//相关日志数目
		$relate_num = 5;
		$tags = $db->result( "SELECT tag FROM `" . DB_PREFIX . "article` WHERE id=$aid" );
		if( $tags != '' ) {
			$tags = str_replace( '*}{*',',',$tags);
			$tags = str_replace( '*}','',$tags);
			$tags = str_replace( '{*','',$tags);
			$tags = split( ',', $tags );
			$sql = '';
			foreach( $tags as $tag ) {
				$sql .= "tag LIKE '%{*$tag*}%' OR ";
			}
			$sql = substr( $sql,0,-4 );
			$sql = "SELECT id,title,type,address,category,content,`read` FROM `" . DB_PREFIX . "article` WHERE ( $sql ) AND id<>$aid AND isdel=0 AND `type`<3 ORDER BY id DESC,`read` DESC LIMIT 0,$relate_num";
			$relate_arr = $db->fetch_all( $sql );
			if( !count( $relate_arr ) ) {
				return '<li>恭喜你找到一篇独一无二的日志 <span class="count">(0)</span> <small class="excerpt">这篇日志暂时还没有相关日志哦！看看随机日志或者最新日志吧。</small></li>';
			}else{
				$output = '';
				foreach( $relate_arr as $relate ) {
					if( $relate['address'] == '' ) $relate['address'] = $relate['id'];
					if( $cache->config['arttype'] == '动态' ) {
						$output .= '<li><a href="' . APP_PATH . 'index.php?m=article&p=' . $relate['id'] . '">' . $relate['title'] . '</a> <span class="count">(' . $relate['read'] . ')</span> <small class="excerpt">' . cut_str( $relate['content'],70 ) . '</small></li>';
					} else {
						$output .= '<li><a href="' . APP_PATH . 'article/' . $cache->category[$relate['category']]['alias'] . '/' . $relate['address'] . '.html">' . $relate['title'] . '</a> <span class="count">(' . $relate['read'] . ')</span> <small class="excerpt">' . cut_str( $relate['content'],70 ) . '</small></li>';
					}
				}
				return $output;
			}
		}else{
			return '<li>恭喜你找到一篇独一无二的日志 <span class="count">(0)</span> <small class="excerpt">这篇日志暂时还没有相关日志哦！看看随机日志或者最新日志吧。</small></li>';
		}
	}

	// 输出评论
	public function show_comment( $aid ){
		global $cache,$db;
		$sql = "SELECT * FROM `" . DB_PREFIX . "comment` WHERE isdel=False AND articleid=$aid ORDER BY cid DESC";
		if( $cache->config['comnum'] > 0 ){
			$sql .= " LIMIT 0," . $cache->config['comnum'];
		}
		$temp = array();
		$temp = $db->fetch_all( $sql );
		if( $temp == array() ){
			return;
		}
		foreach( $temp as $row => $value ){
			$this->theme['comment'] = $value;
			$this->show_comment_de();
		}
	}

	// 评论详细
	public function show_comment_de(){
		If( $this->theme['comment']['email'] <> '' ){
			$gravatar = split( "\|" , $this->theme['comment']['email'] );
			$this->theme['comment']['gravatar'] = '<img src="http://www.gravatar.com/avatar.php?gravatar_id=' . $gravatar[1] . '?s=32&amp;d=wavatar&amp;r=G" height="32" width="32" alt="gravatar" class="gravatar" />';
		}else{
			$this->theme['comment']['gravatar'] = '<img src="' . THEME_PATH . 'images/gravatar.jpg" height="32" width="32" alt="gravatar" class="gravatar" />';
		}
		// 显示控制
		if( $this->theme['comment']['isshow'] == 0 ) {
			$this->theme['comment']['comment'] = '[此评论需要管理员审核后才能显示]';
		} elseif( $this->theme['comment']['isshow'] == 2 ) {
			$this->theme['comment']['comment'] = '[这是一句悄悄话，仅管理员可见]';
		}
		$this->load('comment');
	}

	// 留言
	public function show_guestbook(){
		global $db;
		$offset = ( $this->current_page - 1 ) * $this->max_per_page;
		$sql = "SELECT C.*,U.u_email,U.u_index FROM `" . DB_PREFIX . "comment` C LEFT JOIN `" . DB_PREFIX . "user` U ON U.u_username=C.replyuser WHERE C.isdel=False AND C.articleid = 0 ORDER BY C.cid DESC LIMIT $offset, $this->max_per_page";
		$temp = array();
		$temp = $db->fetch_all( $sql );
		foreach( $temp as $row => $value ){
			$this->theme['comment'] = $value;
			$this->show_comment_de();
		}
	}

	public function get_laodao( $offset = 0, $maxperpage = 0 ) {
		global $db,$theme;

		$admin = $db->fetch_one_array("SELECT * FROM `" . DB_PREFIX . "user` WHERE u_group=8");

		If( $admin['u_email'] <> '' ){
			$gravatar = split( "\|" , $admin['u_email'] );
			$this->theme['laodao']['gravatar'] = '<img src="http://www.gravatar.com/avatar.php?gravatar_id=' . $gravatar[1] . '?s=32&amp;d=wavatar&amp;r=G" height="32" width="32" alt="gravatar" class="gravatar" />';
		}else{
			$this->theme['laodao']['gravatar'] = '<img src="' . THEME_PATH . 'images/gravatar.jpg" height="32" width="32" alt="gravatar" class="gravatar" />';
		}
		$this->theme['laodao']['index'] = $admin['u_index'];
		$this->theme['laodao']['author'] = $admin['u_username'];

		$laodao_arr = $db->fetch_all("SELECT * FROM `" . DB_PREFIX . "laodao` ORDER BY ld_id DESC LIMIT $offset,$maxperpage");
		$output = '';
		foreach( $laodao_arr as $laodao ) {
			$this->theme['laodao']['ld_id'] = $laodao['ld_id'];
			$this->theme['laodao']['ld_time'] = $laodao['ld_time'];
			$this->theme['laodao']['ld_laodao'] = str_replace( array("\r\n", "\n", "\r"),'<br />', htmlspecialchars( $laodao['ld_laodao'] ) );
			$theme->load( 'laodao' );
			$output .= ob_get_contents();
			obclean();
		}
		return $output;
	}

}
?>
